package com.jiuyin.dianjing.util;


import android.text.TextUtils;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import static com.jiuyin.dianjing.util.LogUtil.log;

/**
 *
 */

public class AesUtil {
    private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String CHAR_SET_NAME = "utf-8";
    private static final String KEY = "KJSX19##.D[RTql*";
    private static Cipher mCipher = null;

    static {
        try {
            mCipher = Cipher.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            log("AesUtil exception reason = " + e.getMessage());
        }
    }

    /**
     * @param content  encrypt content
     * @param password key
     * @return String
     */
    public static String encrypt(String content, String password) {
        if (TextUtils.isEmpty(password)) {
            password = KEY;
        }
        try {
            SecretKeySpec key = new SecretKeySpec(password.getBytes(CHAR_SET_NAME), "AES");
            byte[] byteContent = content.getBytes(CHAR_SET_NAME);
            mCipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = mCipher.doFinal(byteContent);
            return Base64.encodeToString(result, Base64.NO_WRAP);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            log("encrypt InvalidKeyException = " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log("encrypt UnsupportedEncodingException = " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            log("encrypt IllegalBlockSizeException = " + e.getMessage());
        } catch (BadPaddingException e) {
            e.printStackTrace();
            log("encrypt BadPaddingException = " + e.getMessage());
        }
        return "";
    }

    /**
     * @param content  decrypt content
     * @param password key
     * @return byte[]
     */
    public static String decrypt(byte[] content, String password) {
        if (TextUtils.isEmpty(password)) {
            password = KEY;
        }
        try {
            SecretKeySpec key = new SecretKeySpec(password.getBytes(CHAR_SET_NAME), "AES");
            Base64.decode(content, Base64.NO_WRAP);
            mCipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = mCipher.doFinal(Base64.decode(content, Base64.NO_WRAP));
            return new String(result);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            log("decrypt InvalidKeyException = " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            log("decrypt IllegalBlockSizeException = " + e.getMessage());
        } catch (BadPaddingException e) {
            e.printStackTrace();
            log("decrypt BadPaddingException = " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log("decrypt UnsupportedEncodingException = " + e.getMessage());
        }
        return "";
    }
}